package com.cluster.platform.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.cluster.platform.common.api.CommonResult;
import com.cluster.platform.common.api.DictionaryEnums;
import com.cluster.platform.common.api.ResultCode;
import com.cluster.platform.dao.CmsContentsDetailDao;
import com.cluster.platform.dto.CmsContentDetailDto;
import com.cluster.platform.dto.CmsContentDetailsDto;
import com.cluster.platform.mapper.CmsContentDetailsMapper;
import com.cluster.platform.mapper.CmsContentProfileMapper;
import com.cluster.platform.mapper.UmsAdminMapper;
import com.cluster.platform.model.CmsContentDetails;
import com.cluster.platform.model.CmsContentProfile;
import com.cluster.platform.model.UmsAdmin;
import com.cluster.platform.service.CmsContentProfileService;
import com.cluster.platform.service.CmsContentsInfoService;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Optional;
import java.util.UUID;

/**
 * @ClassName: CmsContentsInfoServiceImpl
 * @Author: xiezhongping
 * @Date: 2021/1/22 18:02
 * @修改日志
 */
@Service
public class CmsContentsInfoServiceImpl implements CmsContentsInfoService {

    @Autowired
    CmsContentDetailsMapper cmsContentDetailsMapper;

    @Autowired
    CmsContentsDetailDao cmsContentsDetailDao;

    @Autowired
    CmsContentProfileMapper cmsContentProfileMapper;

    @Autowired
    UmsAdminMapper umsAdminMapper;

    @Autowired
    CmsContentProfileService cmsContentProfileService;

    @Override
    @Transactional
    public CommonResult<Object> addCmsContentsInfo(CmsContentDetailsDto cmsContentDetailsDto) {
        if (!Optional.ofNullable(cmsContentDetailsDto).isPresent()) {
            return CommonResult.failed(ResultCode.PARAM_NULL);
        }
        // 通过用户名查找用户id
        UmsAdmin umsAdmin = umsAdminMapper.searchUmsAdminByUserName(cmsContentDetailsDto.getInsertUser());
        if (!Optional.ofNullable(umsAdmin).isPresent()) {
            return CommonResult.failed(ResultCode.USER_NULL);
        }
        cmsContentDetailsDto.setUserId(String.valueOf(umsAdmin.getId()));
        CmsContentProfile cmsContentProfile = BeanUtil.copyProperties(cmsContentDetailsDto, CmsContentProfile.class);
        // 新增文章概要信息 查询新增id
        cmsContentProfile.setInsertDate(new Date());
        cmsContentProfile.setStatus(DictionaryEnums.USABLE.getCode());
        cmsContentProfile.setUserId(cmsContentDetailsDto.getUserId());
        CommonResult<Object> addContentProfile = cmsContentProfileService.addContentProfile(cmsContentProfile);
        if (addContentProfile.getCode() != 200)
            return CommonResult.failed(ResultCode.ADD_FAIL);
        CmsContentProfile addNewCmsContentProfile = BeanUtil.copyProperties(addContentProfile.getData(), CmsContentProfile.class);
        CmsContentDetails cmsContentDetails = BeanUtil.copyProperties(cmsContentDetailsDto, CmsContentDetails.class);
        cmsContentDetails.setContentId(addNewCmsContentProfile.getId());
        cmsContentDetails.setInsertDate(new Date());
        cmsContentDetails.setInsertUser(cmsContentDetailsDto.getUserId());
        cmsContentDetails.setStatus(DictionaryEnums.USABLE_STR.getCode());
        int addStatus = cmsContentDetailsMapper.insert(cmsContentDetails);
        return addStatus > 0 ? CommonResult.success(ResultCode.ADD_SUCCESS) : CommonResult.failed(ResultCode.ADD_FAIL);
    }

    @Override
    public CommonResult<Object> delCmsContentsInfo(String id) {
        return null;
    }

    @Override
    public CommonResult<Object> updateCmsContentsInfo(CmsContentDetailsDto cmsContentDetailsDto) {
        return null;
    }

    @Override
    public CommonResult<Object> searchContentDetail(String contentId) {
        if (StringUtils.isBlank(contentId)) {
            return CommonResult.failed(ResultCode.PARAM_NULL);
        }
        CmsContentDetails cmsContentDetails = cmsContentDetailsMapper.searchContentDetail(contentId);
        if (!Optional.ofNullable(cmsContentDetails).isPresent()) {
            return CommonResult.failed(ResultCode.SEARCH_FAIL);
        }
        return CommonResult.success(cmsContentDetails);
    }

    @Override
    public CommonResult<Object> searchCmsContentInfo(String id) {
        if (StringUtils.isBlank(id)) {
            return CommonResult.failed(ResultCode.PARAM_NULL);
        }
        CmsContentProfile cmsContentProfile = cmsContentProfileMapper.selectByPrimaryKey(id);
        if (!Optional.ofNullable(cmsContentProfile).isPresent()) {
            return CommonResult.failed(ResultCode.SEARCH_FAIL);
        }
        return CommonResult.success(cmsContentProfile);
    }

    @Override
    public CommonResult<Object> searchCmsContent(String id) {
        if (StringUtils.isBlank(id)) {
            return CommonResult.failed(ResultCode.PARAM_NULL);
        }
        CmsContentProfile cmsContentProfile = cmsContentProfileMapper.selectByPrimaryKey(id);
        if (!Optional.ofNullable(cmsContentProfile).isPresent()) {
            return CommonResult.failed(ResultCode.SEARCH_FAIL);
        }
        CmsContentDetailDto cmsContentDetailDto = BeanUtil.copyProperties(cmsContentProfile, CmsContentDetailDto.class);
        CmsContentDetails cmsContentDetails = cmsContentDetailsMapper.searchContentDetail(id);
        if (!Optional.ofNullable(cmsContentDetails).isPresent()) {
            return CommonResult.failed(ResultCode.SEARCH_FAIL);
        }
        cmsContentDetailDto.setContentDetails(cmsContentDetails.getContentDetails());
        cmsContentDetailDto.setEditType(cmsContentDetails.getEditType());
        return CommonResult.success(cmsContentDetailDto);
    }
}
